package com.hhq.sso.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.common.page.Query;
import com.hhq.sso.system.entity.Region;
import com.hhq.sso.system.service.IRegionService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 行政区划表 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/region")
@Tag(name = "行政区划表", description = "行政区划表接口")
public class RegionController {

    private final IRegionService regionService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入region")
    public CommonResult<Region> detail(Region region) {
        Region detail = regionService.getOne(Condition.getQueryWrapper(region));
        return CommonResult.data(detail);
    }

    /**
     * 分页 行政区划表
     */
    @GetMapping("/list")
    @Operation(summary = "分页", description = "传入region")
    public CommonResult<IPage<Region>> list(Region region, Query query) {
        IPage<Region> pages = regionService.page(Condition.getPage(query), Condition.getQueryWrapper(region));
        return CommonResult.data(pages);
    }

    /**
     * 懒加载列表
     */
    @GetMapping("/lazy-list")
    @Parameters({
        @Parameter(name = "code", description = "区划编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "name", description = "区划名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "懒加载列表", description = "传入menu")
    public CommonResult<List<Region>> lazyList(String parentCode,
        @Parameter(hidden = true) @RequestParam Map<String, Object> menu) {
        List<Region> list = regionService.lazyList(parentCode, menu);
        return CommonResult.data(list);
    }

    /**
     * 懒加载列表
     */
    @GetMapping("/lazy-tree")
    @Parameters({
        @Parameter(name = "code", description = "区划编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "name", description = "区划名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "懒加载列表", description = "传入menu")
    public CommonResult<List<Region>> lazyTree(String parentCode,
        @Parameter(hidden = true) @RequestParam Map<String, Object> menu) {
        List<Region> list = regionService.lazyTree(parentCode, menu);
        return CommonResult.data(list);
    }

    /**
     * 新增 行政区划表
     */
    @PostMapping("/save")
    @Operation(summary = "新增", description = "传入region")
    public CommonResult<Boolean> save(@Valid @RequestBody Region region) {
        return CommonResult.status(regionService.save(region));
    }

    /**
     * 修改 行政区划表
     */
    @PostMapping("/update")
    @Operation(summary = "修改", description = "传入region")
    public CommonResult<Boolean> update(@Valid @RequestBody Region region) {
        return CommonResult.status(regionService.updateById(region));
    }

    /**
     * 新增或修改 行政区划表
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入region")
    public CommonResult<Boolean> submit(@Valid @RequestBody Region region) {
        return CommonResult.status(regionService.submit(region));
    }

    /**
     * 删除 行政区划表
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入主键")
    public CommonResult<Boolean> remove(@Parameter(description = "主键", required = true) @RequestParam String id) {
        return CommonResult.status(regionService.removeRegion(id));
    }

    /**
     * 行政区划下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "下拉数据源", description = "传入tenant")
    public CommonResult<List<Region>> select(@RequestParam(required = false, defaultValue = "00") String code) {
        List<Region> list = regionService.list(Wrappers.<Region>query().lambda().eq(Region::getParentCode, code));
        return CommonResult.data(list);
    }

}
